package com.yan.service;

import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import com.yan.comm.DateUtil;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.EnableRetry;

@EnableRetry
@Service
public class TryableService {
	int j = 10;

	@Retryable(value = {
			ArithmeticException.class }, maxAttempts = 3, backoff = @Backoff(delay = 3000l, multiplier = 1))
	public void call() throws Exception {
		System.out.println("do something...");

		int i = 5;
		int j = 1;

		j = i / 0;

		System.out.println("j=== " + j);
		throw new ArithmeticException();
	}

	@Retryable(value = {
			ArithmeticException.class }, maxAttempts = 3, backoff = @Backoff(delay = 3000l, multiplier = 1))
	public String testStr() throws Exception {
		System.out.println("do something...");

		int i = 5;
		int j = 1;

		j = i / 0;
	
		System.out.println("j=== " + j);
		throw new ArithmeticException();
	}

	@Recover
	public void recover(ArithmeticException e) {
		j = 19;
		System.out.println(e.getMessage());
	}

	
	@Autowired
	private DateUtil dataUtil;
	
	public String  getCurrentDate(){
		return dataUtil.formatDate(new Date(), "YYYY-MM-DD HH:MM:SS")	;
	}
	
}
